﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace aQuery
{
    public class PropositionReader<T> : ActionReaderChild<T>, IExecutable where T : class
    {

        #region public members
        public void ExecuteYourself()
        {
            switch (ConditionUsage)
            {
                case ConditionUsage.While:
                    executeWhile();
                    break;
                case ConditionUsage.If:
                    executeIf();
                    break;
            }
        }
        #endregion

        #region internal members
        internal ConditionUsage ConditionUsage { get; set; }

        internal List<ConditionAtTarget<T>> Conditions { get; set; }

        internal PropositionReader(ActionReader<T> target, ConditionUsage conditionUsage, ConditionAtTarget<T> starterCondition) : base(target)
        {
            this.ConditionUsage = conditionUsage;

            this.Conditions = new List<ConditionAtTarget<T>>();
            this.Conditions.Add(starterCondition);
        }

        internal bool IsTrue
        {
            get { return Conditions.All(x => x.Invoke(this.Target) == true); }
        }

        #endregion

        #region private members
        private void executeIf()
        {
            if (this.IsTrue)
            {
                ActionReader.Execute();
            }
        }

        private void executeWhile()
        {
            while (this.IsTrue)
            {
                ActionReader.Execute();
            }
        }
        #endregion

    }
}
